#include "shape.h"

Shape::Shape(int type, QObject* parent, const QColor& color, int penWidth)
    : QObject(parent),
      m_Type(type),
      m_penColor(color),
      m_nPenWidth(penWidth)
{

}

Shape::~Shape()
{
    //qDebug() << __func__;
}



//class Line
Line::Line(QObject* parent, const QColor &color, int penWidth)
    :Shape(line, parent, color, penWidth)
{

}

Line::~Line()
{

}

void Line::Paint(QPainter *painter)
{
    if (painter)
    {
        painter->save();
        painter->setRenderHint(QPainter::Antialiasing, true);
        QPen pen(m_penColor);
        pen.setCapStyle(Qt::RoundCap);
        pen.setJoinStyle(Qt::RoundJoin);
        pen.setWidth(m_nPenWidth);
        painter->setPen(pen);
        int nSize = m_points.size();
        for (int i=1; i<nSize; ++i)
        {
            painter->drawLine(QLine(m_points.at(i-1), m_points.at(i)));
        }

        painter->restore();
    }
}

void Line::SetStartPoint(QPoint pt)
{
    m_points.clear();
    m_points.push_back(pt);
}

void Line::SetEndPoint(QPoint pt)
{
    m_points.push_back(pt);
}


//class Rect
Rect::Rect(QObject *parent, const QColor &color, int penWidth)
    : Shape(rect, parent, color, penWidth),
      m_bHasEnd(false)
{

}

Rect::~Rect()
{

}

void Rect::Paint(QPainter *painter)
{
    if (painter && m_bHasEnd)
    {
        painter->save();
        QPen pen(m_penColor);
        pen.setWidth(m_nPenWidth);
        painter->setPen(pen);
        painter->drawRect(QRect(ptStart, ptEnd));
        painter->restore();
    }
}

void Rect::SetStartPoint(QPoint pt)
{
    ptStart = pt;
}

void Rect::SetEndPoint(QPoint pt)
{
    ptEnd = pt;
    m_bHasEnd = true;
}

LineText::LineText(QObject *parent, const QColor &color, const QFont &font)
    : Shape(text, parent, color, 0),
      m_font(font),
      m_bEdit(true),
      m_nWidth(5),
      m_nHeight(10),
      m_nCursorPos(0),
      m_nCursorX(0),
      m_nAscent(0),
      m_textLayout("")
{
    UpdateTextRect();
}

LineText::~LineText()
{

}

void LineText::Paint(QPainter *painter)
{
    if (!painter)
    {
        return;
    }
    painter->save();
    painter->setFont(m_font);
    QPen pen;
    pen.setColor(m_penColor);
    pen.setStyle(Qt::DashDotLine);
    painter->setPen(pen);
    painter->setRenderHint(QPainter::TextAntialiasing);
    //painter->drawText(QRect(ptStart, QSize(m_nWidth, m_nHeight)).adjusted(-1,-2,1,20), m_text);
    //painter->drawText(QPoint(ptStart.x(), ptStart.y()+m_nAscent), m_text);

    // 绘制文本
    m_textLayout.draw(painter, ptStart);
    if (m_bEdit)
    {
        //绘制外框
        //QPoint point(ptStart.x(), ptStart.y() + );
        //painter->drawRect(QRect(ptStart, QSize(m_nWidth, m_nHeight)).adjusted(-4,-4,4,4));
        painter->drawRect(QRect(ptStart, QSize(m_textLayout.lineAt(0).naturalTextWidth(), m_textLayout.lineAt(0).height())).adjusted(-4,-4,4,4));
        //绘制光标
        static bool ShowCursor = true;
        if (ShowCursor)
        {
            pen = painter->pen();
            pen.setColor(Qt::black);
            pen.setStyle(Qt::SolidLine);
            painter->setPen(pen);
            //painter->drawLine(ptStart.x()+m_nCursorX, ptStart.y(), ptStart.x()+m_nCursorX, ptStart.y() + m_nHeight);
            m_textLayout.drawCursor(painter, ptStart, m_nCursorPos);
        }
        ShowCursor = !ShowCursor;
    }
    //qDebug() << "LineText::Paint text:" << m_text;

    painter->restore();
}

void LineText::SetStartPoint(QPoint pt)
{
    ptStart = pt;
}

void LineText::SetEndPoint(QPoint pt)
{
    Q_UNUSED(pt)
}

void LineText::SetFont(const QFont &font)
{
    m_font = font;
}

void LineText::AppendText(const QString &text)
{
    if (m_nCursorPos == m_text.length())
    {
        //追加字符串
        m_text.append(text);
    }
    else if (m_nCursorPos < m_text.length())
    {
        //插入字符串
        m_text.insert(m_nCursorPos, text);
    }
    m_nCursorPos += text.length();
    UpdateTextRect();
}

void LineText::SetEdit(bool bEdit)
{
    m_bEdit = bEdit;
}

int LineText::TextLength() const
{
    return m_text.length();
}

void LineText::MoveCursor(int step)
{
    if (step > 0)
    {
        m_nCursorPos = (int(m_nCursorPos + step) > m_text.length()) ? m_nCursorPos : (m_nCursorPos + step);
    }
    else
    {
        m_nCursorPos = (int(m_nCursorPos + step) < 0) ? m_nCursorPos : (m_nCursorPos + step);
    }
    UpdateCursorPos();
}

void LineText::BackSpace()
{
    if (!m_text.isEmpty())
    {
        if (m_nCursorPos > 0)
        {
            m_text.remove(m_nCursorPos-1, 1);
            m_nCursorPos--;
            UpdateTextRect();
        }
    }
}

void LineText::Delete()
{
    if (!m_text.isEmpty())
    {
        if (m_nCursorPos < m_text.length())
        {
            m_text.remove(m_nCursorPos, 1);
            UpdateTextRect();
        }
    }
}

void LineText::Home()
{
    if (!m_text.isEmpty())
    {
        m_nCursorPos = 0;
        UpdateCursorPos();
    }
}

void LineText::End()
{
    if (!m_text.isEmpty())
    {
        m_nCursorPos = m_text.length();
        UpdateCursorPos();
    }
}

void LineText::UpdateTextRect()
{
    UpdateLayout();
    QFontMetrics fontMetrics(m_font);
    QString str = m_text.isEmpty() ? QString("A") : m_text;
    int nWidth = fontMetrics.tightBoundingRect(str).width();
//    for (int i=0; i<str.length(); i++)
//    {
////        QRect textRect = fontMetrics.boundingRect(str[i]);
////        nWidth += textRect.width();;
//        nWidth += fontMetrics.horizontalAdvance(str[i]);
//    }
    m_nWidth = nWidth;
    m_nHeight = fontMetrics.height();
    m_nAscent = fontMetrics.ascent();
    UpdateCursorPos();
}

void LineText::UpdateCursorPos()
{
    if (m_nCursorPos > 0)
    {
        QString str = m_text.left(m_nCursorPos);
        QFontMetrics fontMetrics(m_font);
        QRect textRect = fontMetrics.boundingRect(str);
        m_nCursorX = textRect.width();
        //m_nHeight = textRect.height();
    }
    else
    {
        m_nCursorX = 0;
    }
}

void LineText::UpdateLayout()
{
    m_textLayout.clearLayout();
    m_textLayout.setText(m_text);
    m_textLayout.setFont(m_font);
    QFontMetrics fontMetrics(m_font);
    int leading = fontMetrics.leading();
    qreal height = 0;
    //m_textLayout.setCacheEnabled(true);
    // 设置文本选项（如对齐方式）
    QTextOption option;
    option.setAlignment(Qt::AlignLeft);
    m_textLayout.setTextOption(option);
    m_textLayout.beginLayout();
    //while (1) {
      QTextLine textline = m_textLayout.createLine();
//      if (!line.isValid())
//          break;

      textline.setLineWidth(/*lineWidth*/1000);
      height += leading;
      textline.setPosition(QPointF(0, height));
      height += textline.height();
    //}
    m_textLayout.endLayout();
}

